package com.usefullc.crawler.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.enums.YesNoEnum;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.crawler.constants.CrawlerCacheConstants;
import com.usefullc.crawler.domain.Active;
import com.usefullc.crawler.domain.ActiveInstance;
import com.usefullc.crawler.domain.Template;
import com.usefullc.crawler.mapper.ActiveMapper;
import com.usefullc.crawler.query.ActiveQuery;
import com.usefullc.crawler.vo.ActiveVo;
import com.usefullc.crawler.vo.TaskTypeVo;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.service.AbstractBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫活动服务类
 *
 * @author: 星空
 * @date: 2022-07-27
 */
@Service
public class ActiveService extends AbstractBizService<ActiveMapper, Active, ActiveQuery> {

    @Resource
    private RedisTemplate<String, TaskTypeVo> redisTemplate;

    @Autowired
    private TemplateService templateService;

    @Override
    protected QueryWrapper<Active> getQueryWrapper(ActiveQuery query) {
        QueryWrapper<Active> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }

    public Pager<ActiveVo> findVoByPage(ActiveQuery query) {
        Pager<Active> page = this.findByPage(query);
        Pager<ActiveVo> newPage = new Pager<>(page);
        List<Active> dataList = page.getDataList();
        List<ActiveVo> newDataList = new ArrayList<>();
        newPage.setDataList(newDataList);
        for (Active active : dataList) {
            ActiveVo activeVo = BeanUtils.beanCopy(active, ActiveVo.class);
            newDataList.add(activeVo);
        }
        return newPage;
    }

    public List<ActiveVo> findVoAll(ActiveQuery query) {
        List<Active> dataList = this.findAll(query);
        List<ActiveVo> newDataList = new ArrayList<>();
        for (Active active : dataList) {
            ActiveVo activeVo = BeanUtils.beanCopy(active, ActiveVo.class);
            newDataList.add(activeVo);
        }
        return newDataList;
    }



//    private void setTaskCache(Active entity) {
//        Template template = templateService.findById(entity.getTemplateId());
//        String runRdMs = template.getRunRdMs();
//        Long waitTime = Long.valueOf(runRdMs.split("-")[0]);
//
//        //创建任务缓存
//        String name = template.getName()+"-"+entity.getName();
//        TaskTypeVo taskTypeVo = new TaskTypeVo();
//        taskTypeVo.setName(name);
//        taskTypeVo.setWaitTime(waitTime);
//        taskTypeVo.setTaskId(entity.getId());
//        taskTypeVo.setLastTime(System.currentTimeMillis());
////        taskTypeVo.setStatus(YesNoEnum.YES.getCode());
//
//        redisTemplate.opsForHash().putIfAbsent(CrawlerCacheConstants.CRAWLER_TASK_TYPE, entity.getId().toString(), taskTypeVo);
//
//    }

    public boolean saveEntity(Active entity) {
        boolean result = super.save(entity);
//        setTaskCache(entity);
        return result;
    }
    public boolean updateEntity(Active entity) {
//        setTaskCache(entity);
        return super.update(entity);
    }

    public boolean saveEntityList(List<Active> entityList){
        for (Active active : entityList) {
            saveEntity(active);
        }
        return true;
    }


    public void copy(Long id) {
        Active active = this.findById(id);
        active.setId(null);
        active.setName(active.getName() + "_copy");
        active.setOrderNo(active.getOrderNo() + 1);
        this.save(active);
    }

    public void updateStatus(Long id,Integer status){
        //关掉status
        Active active = this.findById(id);

//        //创建任务缓存
//        TaskTypeVo taskTypeVo = (TaskTypeVo) redisTemplate.opsForHash().get(CrawlerCacheConstants.CRAWLER_TASK_TYPE, active.getId().toString());
////        taskTypeVo.setStatus(status);
//
//        redisTemplate.opsForHash().put(CrawlerCacheConstants.CRAWLER_TASK_TYPE, active.getId().toString(), taskTypeVo);

        active.setStatus(status);

        this.save(active);

    }

}
